﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Collections.Specialized;
using System.IO;

namespace braklimitu.App_Code
{
    public class RapidgatorAgent : Agent
    {
        private static string LoginUrl = "https://rapidgator.net/auth/login";
        private static string LogoutUrl = "http://rapidgator.net/auth/logout";
        protected String Url { get; set; } //link wklejony
        protected String FID { get; set; } // file id, id pliku pobieranego
        protected String Username { get; set; } //login premium
        protected String Password { get; set; } //pass premium
        protected String DirectUrl { get; set; } //link do pobierania bezposredniego
        protected CookieCollection cookie; //kolekcja ciasteczek
        protected long FileSize { get; set; } //rozmiar pobieranego pliku
        protected String FileName { get; set; } //nazwa pliku
        public String ResponseLogin { get; set; } //do testow
        protected String ButtonUrl { get; set; } //link przycisku to wywołania pobierania

        public RapidgatorAgent(String Url, String Username, String Password)
        {
            this.Url = Url;
            this.Username = Username;
            this.Password = Password;
            this.DirectUrl = String.Empty;
            this.FileName = String.Empty;
            this.ButtonUrl = Url + "#";
            this.FileSize = 0;
            cookie = new CookieCollection();
            //FID:-----
            int begI = Url.IndexOf("/file/") + 6;
            int endI = Url.LastIndexOf("/");
            FID = Url.Substring(begI, endI - begI);
            //---------

        }

        public RapidgatorAgent(String Url)
        {
            this.Url = Url;
            this.DirectUrl = String.Empty;
            this.FileName = "";
        }

        public override bool LogIn()
        {
            string postData = "LoginForm%5Bemail%5D=" + System.Web.HttpUtility.HtmlEncode(Username) + "&LoginForm%5Bpassword%5D=" + System.Web.HttpUtility.HtmlEncode(Password);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://rapidgator.net/auth/login");
            webRequest.Method = "GET";
            webRequest.CookieContainer = new CookieContainer();
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            cookie.Add(response.Cookies);
            
            webRequest = (HttpWebRequest)WebRequest.Create(LoginUrl);
            webRequest.Method = "POST";
            webRequest.AllowAutoRedirect = false;
            webRequest.CookieContainer = new CookieContainer();
            webRequest.CookieContainer.Add(cookie);
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = postData.Length;
            webRequest.AllowAutoRedirect = false;
            using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
            {
                requestWriter2.Write(postData);
            }

            response = (HttpWebResponse)webRequest.GetResponse();
            
            string responseData = string.Empty;

            for (int i = 0; i < response.Headers.Count; i++)
            {
                System.Diagnostics.Debug.WriteLine(response.Headers.GetKey(i) + ":" + string.Join(",", response.Headers.GetValues(i)));
            }
            
            using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                responseData = responseReader.ReadToEnd();
            }
            cookie.Add(response.Cookies);

            if (responseData.ToString().Contains("Premium till") && responseData.ToString().Contains(Username))
                return true;
            return false;
        }

        public override bool LogOut()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(LogoutUrl);
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            string responseData = string.Empty;


            using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                responseData = responseReader.ReadToEnd();
            }
            return true;
        }

        public override DownloadLink GetFileInfo()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
            webRequest.CookieContainer = new CookieContainer();
            webRequest.CookieContainer.Add(cookie);
            string responseData = string.Empty;
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                responseData = responseReader.ReadToEnd();
            }
            
            return null;
        }

    }
}